const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        invoiceCon: ['酒'],
        currentTab: 0,
        showAddInv: 0,
        currTab: 0,
        invoiceIndex: 0,
        invType: 'person',
        inv_title: ''
    },
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
        var self = this
        wx.getStorage({
            key: 'key',
            success: function (res) {
                self.setData({
                    key: res.data
                })
            }
        })
    },
    invoiceCon: function (e) {
        this.setData({
            invoiceIndex: e.detail.value
        })
    },
    changeCur: function (e) {
        var self = this;
        this.setData({
            currentTab: e.target.dataset.current,
        })
        if (e.target.dataset.current == 1) {
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_invoice&op=invoice_content_list',
                data: {
                    key: self.data.key
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    self.setData({
                        invoice_content_list: res.data.datas.invoice_content_list
                    })
                }
            })
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_invoice&op=invoice_list',
                data: {
                    key: self.data.key
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    self.setData({
                        invoice_list: res.data.datas.invoice_list
                    })
                }
            })
        }
    },
    changeCurrent: function (e) {
        this.setData({
            currTab: e.target.dataset.curr,
            invType: e.target.dataset.invtype
        })
    },
    addInvoice: function (e) {
        this.setData({
            showAddInv: e.target.dataset.addinvoice
        })
    },
    invTitle: function (e) {
        this.setData({
            inv_title: e.detail.value
        })
    },
    addInvoiceInfo: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_invoice&op=invoice_add',
            data: {
                key: self.data.key,
                inv_title_select: self.data.invType,
                inv_title: self.data.inv_title,
                inv_content: self.data.invoice_content_list[self.data.invoiceIndex],
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                self.setData({
                    inv_id: res.data.datas.inv_id
                })
            }
        })
    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})